package edu.uc.ui.ctrl25;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.liuvei.common.PagerItem;
import com.liuvei.common.SysFun;

import edu.uc.bean.Goods;
import edu.uc.service.GoodsService;

@Repository("GoodsAction")
@Scope("prototype")
public class GoodsAction  extends BaseAction{
	
	@Autowired
	private GoodsService goodsService;
	private String id;
	private Long goodId;
	private String name;
	private Double price;
	private String descibe;
	private String pictureUrl;
	private Integer inventory;//库存
	private String address;//商品地址
	private String type;//商品类别,运动类，上衣
	private String beizhu;//备注
	
	 private List<File> file;
	 private List<String> fileFileName;
	 private List<String> fileContentType;
	 private List<String> dataUrl;
	public String list(){
		List<Goods> vList = null;
		//��ҳ �������� ����url���ݵ�pagesize �� pageindex
		PagerItem pagerItem = new PagerItem();
		pagerItem.parsePageSize(pageSize);
		pagerItem.parsePageNum(pageNum);
		//��ҳ �������� ���ҷ������������м�¼ 
		Long rowCount = goodsService.count();
		//��ҳ2 ����¼������pagerItem,�Ա���з�ҳ�ĸ������
		pagerItem.changeRowCount(rowCount);
		//��ҳ3 �����ݿ�ȡָ����ҳ������ 
		vList = goodsService.pager(pagerItem.getPageNum(),pagerItem.getPageSize());
		//��ҳ4 ����ҳ����ת��url
		pagerItem.changeUrl(SysFun.generalUrl(requestURI, queryString));
		//��ҳ5 ����ҳ���� �ŵ������� �Ա�ҳ����Է���
		request.put("pagerItem", pagerItem);
		request.put("dataList", vList);
		for(int i = 0; i<vList.size();i++){
			System.out.println(vList.get(i).getAddress());
		}
		return "list";
	} 
	public String listDeal() throws UnsupportedEncodingException{
		
		//����������Ϊ��������
		String str = new String(name.getBytes("iso-8859-1"),"UTF-8");
		request.put("name", str);
		if(!SysFun.isNullOrEmpty(str)){
			name = str.trim();
		}
		else{
			name = "";
		}
		System.out.println(name+123);
		List<Goods> vList = null;
		//��ҳ �������� ����url���ݵ�pagesize �� pageindex
		PagerItem pagerItem = new PagerItem();
		pagerItem.parsePageSize(pageSize);
		pagerItem.parsePageNum(pageNum);
		//��ҳ �������� ���ҷ������������м�¼ 
		Long rowCount = goodsService.countByName(name);
		//��ҳ2 ����¼������pagerItem,�Ա���з�ҳ�ĸ������
		pagerItem.changeRowCount(rowCount);
	
		//��ҳ3 �����ݿ�ȡָ����ҳ������ 
		vList = goodsService.pagerByName(name,pagerItem.getPageNum(),pagerItem.getPageSize());
		//��ҳ4 ����ҳ����ת��url
		pagerItem.changeUrl(SysFun.generalUrl(requestURI, queryString));
		//��ҳ5 ����ҳ���� �ŵ������� �Ա�ҳ����Է���
		request.put("pagerItem", pagerItem);
		request.put("dataList", vList);
		return "list";
	}
	
	
	public String insert(){
		return "insert";
	}
	public String insertDeal() throws IOException{
		request.put("name", name);
		request.put("price", price);
		System.out.println(pictureUrl+123123123);
		String vMsg="";
		if(SysFun.isNullOrEmpty(name)){
			vMsg+="名字不能为空";
		}
		if(price==null){
			vMsg+="价格不能为空";
		}
		if(!SysFun.isNullOrEmpty(vMsg)){
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "insert";
		}
		Goods bean = new Goods();
		bean.setPictureUrl(pictureUrl);
		bean.setAddress(address);
		bean.setBeizhu(beizhu);
		bean.setDescibe(descibe);
		bean.setInventory(inventory);
		bean.setName(name);
		bean.setPictureUrl(pictureUrl);
		bean.setPrice(price);
		bean.setType(type);
		Long result = 0L;
		try{
			result = goodsService.insert(bean);
		}
		catch (Exception e) {
			// TODO: handle exception
			vMsg +="添加失败"+e.getMessage();
		}
		if(result >0){
			return "go_preload";
		}else{
			request.put("msg", vMsg);
			return "insert";
		}
	}
	public String delete(){
		return "go_ok";
	}
	public String deleteDeal(){
		id.valueOf(goodId);
		if(!SysFun.isNullOrEmpty(id)){
			Long iId = SysFun.parseLong(id);
			Long result = goodsService.delete(iId);
			if(result >0){
				return "go_ok";
			}
		
		}
		return "go_no";
	}
	public String update(){
		if(!SysFun.isNullOrEmpty(id)) {
			Long iId = SysFun.parseLong(id);
			Goods bean = goodsService.load(iId);
			if(bean != null) {
				System.out.println(bean.getGoodId());
				request.put("goodId", bean.getGoodId());
				request.put("name", bean.getName());
				request.put("price", bean.getPrice());
				request.put("descibe", bean.getDescibe());
				request.put("inventory", bean.getInventory());
				request.put("address", bean.getAddress());
				request.put("beizhu", bean.getBeizhu());
				request.put("type", bean.getType());
				request.put("pictureUrl", bean.getPictureUrl());
				return "update";	
			}
		}
		return "update";
	}
	public String pictureupdate()throws IOException{
		
			
			
			dataUrl = new ArrayList<String>();
			
			Goods bean = goodsService.load(goodId);
			          String imgpath = "image/";
			         List<String> image = new ArrayList<String>();
			         List<String> picture = new ArrayList<String>();
			          for (int i = 0; i < file.size(); ++i) {
			              InputStream is = new FileInputStream(file.get(i));
			  
			              String path = ServletActionContext.getServletContext().getRealPath("/");
			              System.out.println(path);
			              
			  
			              dataUrl.add(imgpath+""+this.getFileFileName().get(i));
			              File destFile = new File(path+imgpath, this.getFileFileName().get(i));
			              image.add(imgpath+this.getFileFileName().get(i)); 
			              picture.add(this.getFileFileName().get(i));
			              System.out.println(path+imgpath+(this.getFileFileName()).get(i));
			              OutputStream os = new FileOutputStream(destFile);
			  
			              byte[] buffer = new byte[400];
			  
			              int length = 0;
			  
			              while ((length = is.read(buffer)) > 0) {
			                  os.write(buffer, 0, length);
			              }
			
			            is.close();
			
			             os.close();
		        }
			         
			         
			         request.put("pictureUrl", picture.get(0));
			         pictureUrl = picture.get(0);
			         bean.setPictureUrl(pictureUrl);
			         request.put("name", bean.getName());
						request.put("price", bean.getPrice());
						request.put("descibe", bean.getDescibe());
						request.put("inventory", bean.getInventory());
						request.put("address", bean.getAddress());
						request.put("beizhu", bean.getBeizhu());
						request.put("type", bean.getType());
						request.put("pictureUrl", bean.getPictureUrl());
		return "update";
	}
	public String updateDeal(){
		Long result = 0L;
		String vMsg="";
		request.put("goodid", goodId);
		request.put("name", name);
		request.put("price", price);
		request.put("descibe", descibe);
		request.put("inventory", inventory);
		request.put("address", address);
		request.put("beizhu", beizhu);
		if(SysFun.isNullOrEmpty(name)){
			vMsg+="名字不能为空";
		}
		if(SysFun.isNullOrEmpty(price.toString())){
			vMsg+="价格不能为空";
		}
		if(!SysFun.isNullOrEmpty(vMsg)){
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "update";
		}
		System.out.println(goodId+"-------------------");
		System.out.println();
		long goodsid = Long.parseLong(id);
		Goods bean = goodsService.load(goodsid);
		System.out.println(price+"111111");
		System.out.println(descibe+"222222");
		
		if(bean == null){
			vMsg +="没有找到相关数据";
		}
		if(!SysFun.isNullOrEmpty(vMsg)){
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "update";
		}
		bean.setAddress(address);
		bean.setBeizhu(beizhu);
		bean.setDescibe(descibe);
		bean.setInventory(inventory);
		bean.setName(name);
		bean.setPictureUrl(pictureUrl);
		bean.setPrice(price);
		bean.setType(type);
		try{
			result = goodsService.update(bean);
		}
		catch (Exception e) {
			// TODO: handle exception
			vMsg +="更新失败"+e.getMessage();
		}
		if(result >0){
			return "go_preload";
		}else{
			request.put("msg", vMsg);
			return "update";
		}
	}
	public String detail(){
		
		if(!SysFun.isNullOrEmpty(id)) {
			String vMsg="";
			Long iId = SysFun.parseLong(id);
			Goods bean = goodsService.load(iId);
			if(bean != null) {
				request.put("name", bean.getName());
				request.put("price", bean.getPrice());
				request.put("descibe", bean.getDescibe());
				request.put("inventory", bean.getInventory());
				request.put("address", bean.getAddress());
				request.put("beizhu", bean.getBeizhu());
				request.put("type", bean.getType());
				request.put("pictureUrl", bean.getPictureUrl());
				return "detail";	
			}
			else{
				vMsg+="查询失败";
			}
		}
		return "detail";

	}
	
	public String pictureAdd()throws IOException{
		
		
		dataUrl = new ArrayList<String>();
		          
		          String imgpath = "image/";
		         List<String> image = new ArrayList<String>();
		         List<String> picture = new ArrayList<String>();
		          for (int i = 0; i < file.size(); ++i) {
		              InputStream is = new FileInputStream(file.get(i));
		  
		              String path = ServletActionContext.getServletContext().getRealPath("/");
		              System.out.println(path);
		              
		  
		              dataUrl.add(imgpath+""+this.getFileFileName().get(i));
		              File destFile = new File(path+imgpath, this.getFileFileName().get(i));
		              image.add(imgpath+this.getFileFileName().get(i)); 
		              picture.add(this.getFileFileName().get(i));
		              System.out.println(path+imgpath+(this.getFileFileName()).get(i));
		              OutputStream os = new FileOutputStream(destFile);
		  
		              byte[] buffer = new byte[400];
		  
		              int length = 0;
		  
		              while ((length = is.read(buffer)) > 0) {
		                  os.write(buffer, 0, length);
		              }
		
		            is.close();
		
		             os.close();
	        }
		         
		         request.put("pictureUrl", picture.get(0));
		         pictureUrl = picture.get(0);
		         System.out.println(picture.get(0));
		         return "insert";
		      }
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public Long getGoodId() {
		return goodId;
	}
	public void setGoodId(Long goodId) {
		this.goodId = goodId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public String getDescibe() {
		return descibe;
	}
	public void setDescibe(String descibe) {
		this.descibe = descibe;
	}
	public String getPictureUrl() {
		return pictureUrl;
	}
	public void setPictureUrl(String pictureUrl) {
		this.pictureUrl = pictureUrl;
	}
	public Integer getInventory() {
		return inventory;
	}
	public void setInventory(Integer inventory) {
		this.inventory = inventory;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getBeizhu() {
		return beizhu;
	}
	public void setBeizhu(String beizhu) {
		this.beizhu = beizhu;
	}
	public List<File> getFile() {
		return file;
	}
	public void setFile(List<File> file) {
		this.file = file;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}





}
